<?php
session_start();

include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

if (isset($_REQUEST['code'])) {
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken('code', $keys) ;
	} catch (OAuthException $e) {
		echo $e->getMessage();
	}
}

if ($token)
{
	$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token['access_token'] );
	$uid_get = $c->get_uid();
	$uid = $uid_get['uid'];
	$user_info = $c->show_user_by_id( $uid);
	
	$_SESSION['token'] = $token;
	$_SESSION['uid'] = $uid;
	$_SESSION['name'] = $user_info["screen_name"];
	$_SESSION['friends_count'] = $user_info["bi_followers_count"];
	$_SESSION['photo'] = $user_info["profile_image_url"];
?>

<script language="javascript">
	parent.JielongCNY.jielongApp.loginComplete({uid:"<?php echo $_SESSION['uid']?>",
												name:"<?php echo $_SESSION['name']?>",
												friends_count:<?php echo $_SESSION['friends_count']?>,
												photo:"<?php echo $_SESSION['photo']?>"});
</script>
<?php
}else{
?>
<script language="javascript">
	parent.JielongCNY.jielongApp.removePopup();
</script>
<?php
}
?>

